/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.myapp.struts.Usuario;
import com.myapp.struts.UsuarioHelper;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author ramiro
 */
public class send extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("hola hijo mio");
        String Box = request.getParameter("Box");
        String smtp    = "smtp.gmail.com";
        String From    = "ramiroso217@gmail.com";
        String infor   = "forever217";
        String To       = "ramiro_darkness2117@hotmail.com";
        
        String asunto  = "mensaje recordatorio";
        String mensaje = request.getParameter("mensaje");
        System.out.print("-------"+mensaje);
        //---------
        
        String tipo = request.getParameter("Box");
        UsuarioHelper helper = new UsuarioHelper();
        List<Usuario> l = helper.getUsers();
        List<Usuario> lista;
        if (tipo.equals("Todos")){lista = l;}
               else{lista = helper.categoriaUsuarios(tipo, l);} 
        
        if (Box.equals("Todos")){
            if (!lista.isEmpty()) {
            Iterator i = lista.iterator();
            while (i.hasNext()) {
                Usuario u = (Usuario) i.next();
                Enviar.send(smtp,From,infor,u.getCorreo(),asunto,mensaje);
            }
            }
        }
        else{
            if (!lista.isEmpty()) {
            Iterator i = lista.iterator();
            while (i.hasNext()) {
                Usuario u = (Usuario) i.next();
                if (tipo.equals(Box))
                Enviar.send(smtp,From,infor,u.getCorreo(),asunto,mensaje);
            }
            }
            Enviar.send(smtp,From,infor,To,asunto,mensaje);
        }

        return mapping.findForward(SUCCESS);
    }
}
